/*
 * @Description: 
 * @version: 
 * @Author: 171
 * @Date: 2022-08-31 10:29:55
 * @LastEditors: 171
 * @LastEditTime: 2022-09-22 11:51:54
 */
import { createApp } from 'vue'
import { createPinia } from 'pinia'

import ElementPlus from "element-plus"; // 引入element
import "element-plus/dist/index.css";
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import App from './App.vue'
import router from './router'
import Loading from "./components/Loading/Loading"
import VMdEditor from './v-md-editor'


type Load = {
  show: () => {},
  hide: () => {}
}
declare module "@vue/runtime-core" {
  export interface ComponentCustomProperties {
    $loading: Load; // 编写ts loading 声明文件放置报错 和 智能提示
  }
}
declare global {  //设置全局属性
  interface Window {  //window对象属性
    WIDGET: any;   //加入对象
  }
}

const app = createApp(App)

app.use(router)
app.use(createPinia())
app.use(ElementPlus);

app.use(Loading);
app.use(VMdEditor);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) { // 注册全部图标
  app.component(key, component)
}

const whileList = ['/', '/home','/login', '/epidemic']
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  //白名单有值，或者登陆过存储了token信息可以跳转，否则就去登录页面
  if (whileList.includes(to.path) || token) {
    if(token && (to.path === "/login" || to.path === "/register")) return next(false);
    return next()
  }else {
    if(from.path === '/login') return next(false)
    const message = app.config.globalProperties.$message({
      message: '当前还未登录，即将跳转到登录界面',
      grouping: true,
      onClose: () => {
        next({  // 加入参数时，会形成新的导航，调用回调函数，应该加入条件，且至少包含next()或next(false)，防止无限调用
          path:'/login'
        })
      }
    });
    setTimeout(()=>{
      message.close();
    }, 3000);
  }
})

app.mount('#app')
